package com.lxinet.jeesns.core.controller;

import com.lxinet.jeesns.core.enums.Messages;
import com.lxinet.jeesns.core.exception.JeeException;
import com.lxinet.jeesns.core.exception.ParamException;
import com.lxinet.jeesns.core.utils.JeesnsConfig;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController
{
  @Resource
  protected HttpServletRequest request;
  @Resource
  protected HttpServletResponse response;
  @Resource
  protected JeesnsConfig jeesnsConfig;
  
  protected String getParam(String name)
  {
    return this.request.getParameter(name);
  }
  
  protected String getParam(String name, String defaultValue)
  {
    String value = getParam(name);
    if (null == value) {
      value = defaultValue;
    }
    return value;
  }
  
  protected int getParamToInt(String name)
    throws ParamException
  {
    String value = getParam(name);
    try
    {
      return Integer.parseInt(value);
    }
    catch (Exception e)
    {
      throw new ParamException("��������");
    }
  }
  
  protected int getParamToInt(String name, int defaultValue)
  {
    try
    {
      return getParamToInt(name);
    }
    catch (ParamException paramException) {}
    return defaultValue;
  }
  
  protected double getParamToDouble(String name)
    throws ParamException
  {
    String value = getParam(name);
    try
    {
      return Double.parseDouble(value);
    }
    catch (Exception e)
    {
      throw new ParamException("��������");
    }
  }
  
  protected double getParamToInt(String name, double defaultValue)
  {
    try
    {
      return getParamToDouble(name);
    }
    catch (ParamException paramException) {}
    return defaultValue;
  }
  
  protected String getErrorMessages(BindingResult result)
  {
    StringBuffer errorMessages = new StringBuffer();
    List<FieldError> list = result.getFieldErrors();
    int count = 0;
    for (FieldError error : list)
    {
      errorMessages.append(error.getDefaultMessage());
      count++;
      if (count < list.size()) {
        errorMessages.append("<br/>");
      }
    }
    return errorMessages.toString();
  }
  
  protected String errorModel(Model model, String msg)
  {
    model.addAttribute("msg", msg);
    return "/manage/common/error";
  }
  
  protected boolean isAjaxRequest()
  {
    String header = this.request.getHeader("X-Requested-With");
    return "XMLHttpRequest".equalsIgnoreCase(header);
  }
  
  @ExceptionHandler
  public void execptionHandler(Exception e)
  {
    if (!isAjaxRequest()) {
      try
      {
        e.printStackTrace();
        String msg = "����������" + e.toString();
        if (((e instanceof JeeException)) && (((JeeException)e).getJeeMessage() != null)) {
          msg = ((JeeException)e).getJeeMessage().getMessage();
        }
        msg = URLEncoder.encode(msg, "UTF-8");
        String currUrl = this.request.getRequestURL().toString();
        this.request.setAttribute("msg", msg);
        String redirectUrl;
        if (currUrl.indexOf("/" + this.jeesnsConfig.getManagePath() + "/") > -1) {
          redirectUrl = this.request.getContextPath() + "/" + this.jeesnsConfig.getManagePath() + "/error?msg=" + msg;
        } else {
          redirectUrl = this.request.getContextPath() + "/error?msg=" + msg;
        }
        this.response.sendRedirect(redirectUrl);
      }
      catch (IOException e1)
      {
        e1.printStackTrace();
      }
    }
  }
}
